// pages/homelink/serach_allpro/serach_allpro.js
import gcoord from "../../../../miniprogram_npm/gcoord/dist/gcoord";
import moreConditions from "../../../../utils/moreConditon";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    distance: "1.0",
    longitude: "",
    latitude: "",
    pageNum: 1,
    // 搜索
    serach_pro: "",
    prosum: null,
    prolist: [],
    value1: "1",
    option1: [
      {
        text: "苏州大市",
        value: "1",
      },
      {
        text: "姑苏区",
        value: "2",
      },
      {
        text: "工业园区",
        value: "3",
      },
      {
        text: "高新区",
        value: "4",
      },
      {
        text: "相城区",
        value: "5",
      },
      {
        text: "吴中区",
        value: "6",
      },
      {
        text: "吴江区",
        value: "7",
      },
      {
        text: "昆山市",
        value: "8",
      },
      {
        text: "太仓市",
        value: "9",
      },
      {
        text: "常熟市",
        value: "10",
      },
      {
        text: "张家港市",
        value: "11",
      },
    ],
    option2: [
      {
        text: "全部行业",
        value: "1",
      },
    ],
    hangyeallList: [],
    hangyelistRight: [],
    value2: "1",
    selectLeftindex: 0,
    leftId: "",
    rightId: "",
    selectRightindex: 0,
    // 更多
    option3: [
      //   {
      //   text: "筛选更多",
      //   value: "1"
      // }
    ],
    value3: "1",
    moreConditions: [],
    // 区域
    region: "",
    industy: "",
    // 更多全部条件
    allConditions: {},
  },
  // 搜索
  serachProChange (e) {
    this.setData({
      serach_pro: e.detail,
    });
  },
  goSearch () {
    this.firstSearch()
  },
  firstSearch () {
    this.setData({
      prolist: [],
      prosum: ''
    });
    this.getList()
  },
  toprodetail (e) {
    wx.navigateTo({
      url: `/pages/mapDetail/mapDetail?corpKey=${e.detail.corpKey}&latitude=${e.detail.latitude}&longitude=${e.detail.longitude}&distance=${e.detail.distance}`,
    });
  },
  gethangyeTreelist () {
    var _this = this;
    //console.log(1111);
    getApp()
      .globalData.request(
        "/map/getIndustryTree",
        {
          distance: this.data.distance,
          userLng: _this.data.longitude,
          userLat: _this.data.latitude,
        },
        "POST"
      )
      .then((res) => {
        console.log(res);
        var arr = [];
        var obj = {
          text: "全部行业",
          id: "1",
        };
        arr = res.data;
        arr.unshift(obj);
        var rightarr = [
          {
            text: "全部行业",
            id: "1",
          },
        ];
        _this.setData({
          hangyeallList: arr,
          hangyelistRight: rightarr,
        });
      });
  },
  leftlistClick (e) {
    // console.log(e.currentTarget.dataset.leftlistindex);
    var clickindex = e.currentTarget.dataset.leftlistindex;
    if (clickindex == 1) {
      var arr = [
        {
          text: "全部行业",
          id: "1",
        },
      ];
      this.setData({
        hangyelistRight: arr,
        leftId: "",
        rightId: "",
      });
      this.firstSearch();
      this.selectComponent("#dowente1").toggle(false);
    } else {
      this.data.hangyeallList.forEach((item, index) => {
        if (clickindex == item.id) {
          if (item.children.length !== 0) {
            var arr = [];
            item.children.forEach((i) => {
              let obj = {
                text: i.text,
                value: i.id,
                id: i.id,
              };
              arr.push(obj);
            });
            this.setData({
              hangyelistRight: arr,
              leftId: item.id,
              rightId: item.id,
            });
            this.firstSearch();
          }
        }
      });
    }
    this.setData({
      selectLeftindex: e.currentTarget.dataset.selectindex,
    });
  },
  citySelect (e) {
    // region
    if (e.detail == 1) {
      this.setData({
        region: "",
      });
    } else {
      this.setData({
        region: this.data.option1[e.detail - 1].text,
      });
    }
    this.firstSearch();
  },
  // 选中行业
  rightlistClick (e) {
    // console.log(e.currentTarget.dataset.rightlistindex);
    // 1 = 全部行业
    if (e.currentTarget.dataset.rightlistindex == 1) {
      this.setData({
        rightId: "",
      });
      this.firstSearch();
      this.selectComponent("#dowente1").toggle(false);
    } else {
      this.setData({
        rightId: e.currentTarget.dataset.rightlistindex,
      });
      this.firstSearch();
      this.selectComponent("#dowente1").toggle(false);
    }
    this.setData({
      selectRightindex: e.currentTarget.dataset.selectindex,
    });
  },
  setName (e) {
    // e.currentTarget.dataset.itempre.value = e.currentTarget.dataset.itemped.key;
    this.data.moreConditions.forEach((item, index) => {
      if (item.key == e.currentTarget.dataset.itemkey) {
        item.children.forEach((jtem) => {
          if (jtem.key == e.currentTarget.dataset.itempre.key) {
            jtem.value = e.currentTarget.dataset.itemped.key;
          }
        });
      }
    });
    this.setData({
      moreConditions: this.data.moreConditions,
    });
  },
  // 重置
  merescreenReset () {
    this.data.moreConditions.forEach((i) => {
      i.children.forEach((j) => {
        console.log(j.key + ":" + (j.value == "" ? "1" : j.value));
        j.value = "";
      });
    });
    this.setData({
      moreConditions: this.data.moreConditions,
    });
  },
  // 确定
  merescreenConfirm () {
    var allConditions = {};
    this.data.moreConditions.forEach((i) => {
      i.children.forEach((j) => {
        allConditions[j.key] = j.value;
      });
    });
    if (allConditions.numberOfCompanyUpperLimit) {
      allConditions.numberOfCompanyLowerLimit =
        allConditions.numberOfCompanyUpperLimit.split("_")[0];
      allConditions.numberOfCompanyUpperLimit =
        allConditions.numberOfCompanyUpperLimit.split("_")[1];
    }
    if (allConditions.registeredCapitalUpperLimit) {
      allConditions.registeredCapitalLowerLimit =
        allConditions.registeredCapitalUpperLimit.split("_")[0];
      allConditions.registeredCapitalUpperLimit =
        allConditions.registeredCapitalUpperLimit.split("_")[1];
    }
    if (allConditions.yearsOfEstablishUpperLimit) {
      allConditions.yearsOfEstablishLowerLimit =
        allConditions.yearsOfEstablishUpperLimit.split("_")[0];
      allConditions.yearsOfEstablishUpperLimit =
        allConditions.yearsOfEstablishUpperLimit.split("_")[1];
    }
    if (allConditions.scoreUpperLimit) {
      allConditions.scoreLowerLimit =
        allConditions.scoreUpperLimit.split("_")[0];
      allConditions.scoreUpperLimit =
        allConditions.scoreUpperLimit.split("_")[1];
    }
    if (allConditions.businessIncomeLowerLimit) {
      allConditions.businessIncomeUpperLimit =
        allConditions.businessIncomeLowerLimit.split("_")[1];
      allConditions.businessIncomeLowerLimit =
        allConditions.businessIncomeLowerLimit.split("_")[0];
    }
    console.log(allConditions);
    this.setData({
      allConditions: allConditions,
    });
    this.firstSearch();
    this.selectComponent("#dowente2").toggle(false);
  },

  phone (event) {
    wx.makePhoneCall({
      phoneNumber: event.currentTarget.id,
    });
  },
  location (event) {
    var _this = this;
    wx.openLocation({
      latitude: _this.data.prolist[event.currentTarget.id].latitude,
      longitude: _this.data.prolist[event.currentTarget.id].longitude,
      name: _this.data.prolist[event.currentTarget.id].d2CorpName,
      address: _this.data.prolist[event.currentTarget.id].d2Dom,
      scale: 18,
    });
  },
  // 滑动底部加载更多
  scrollBottom () {
    this.onBottom();
  },
  getList () {
    var _this = this;
    const params = {
      pageBO: {
        pageSize: 50,
        pageNum: 1,
      },
      region: _this.data.region,
      industy: _this.data.leftId + _this.data.rightId,
      searchParam: _this.data.serach_pro,
    };
    var condtion = Object.assign(params, this.data.allConditions);
    console.log(condtion);
    if (_this.data.serach_pro) {
      wx.showLoading({
        title: "加载中",
      });
      getApp()
        .globalData.request("/map/fullAmountCorp", condtion, "POST")
        .then((res) => {
          setTimeout(function () {
            wx.hideLoading();
          }, 500);
          var rstObj = res;
          console.log(res);
          _this.setData({
            prolist: rstObj.data.list,
            prosum: rstObj.data.total,
          });
        });
    } else {
      // wx.showToast({
      //   title: "请先搜索企业！",
      //   icon: "none",
      // });
    }
  },
  getlistgengduo () {
    var _this = this;
    wx.showLoading({
      title: "加载中",
    });
    const params = {
      pageBO: {
        pageSize: 50,
        pageNum: _this.data.pageNum,
      },
      region: _this.data.region,
      industy: _this.data.leftId + _this.data.rightId,
      searchParam: _this.data.serach_pro,
    };
    var condtion = Object.assign(params, this.data.allConditions);
    console.log(condtion);
    getApp()
      .globalData.request("/map/fullAmountCorp", condtion, "POST")
      .then((res) => {
        setTimeout(function () {
          wx.hideLoading();
        }, 500);
        var rstObj = res;
        console.log(res);
        _this.setData({
          prolist: _this.data.prolist.concat(rstObj.data.list),

          prosum: rstObj.data.total,
        });
      });
  },
  onBottom () {
    this.data.pageNum++;
    this.getlistgengduo();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    console.log(options.serachpro);
    this.setData({
      serach_pro: options.serachpro
    })
    this.gethangyeTreelist();
    this.getList()
    this.setData({
      moreConditions: moreConditions.arr,
    });

    // 获取附近企业
    var _this = this;
    getApp().globalData.getLocation({
      type: "gcj02",
      success (res) {
        _this.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        });
        var resae = gcoord.transform(
          [res.longitude, res.latitude],
          gcoord.GCJ02, // 目标坐标系
          gcoord.BD09 // 当前坐标系
        );
        // console.log(resae);
        _this.setData({
          longitude: resae[0],
          latitude: resae[1],
        });

        // wx.showLoading({
        //   title: '加载中',

        // })
        // const params = {
        //   distance: _this.data.distance,
        //   userLng: _this.data.longitude,
        //   userLat: _this.data.latitude,
        //   pageBO: {
        //     pageSize: 50,
        //     pageNum: 1
        //   },
        //   searchParam:"苏州"
        // }
        // getApp().globalData.request('/map/fullAmountCorp', params, "POST", ).then(res => {
        //   setTimeout(function () {
        //     wx.hideLoading()
        //   }, 500)
        //   var rstObj = res
        //   console.log(res);

        //   for (let p = 0; p < rstObj.data.length; p++) {
        //     var resae = gcoord.transform(
        //       [rstObj.list[p].longitude, rstObj.list[p].latitude],
        //       gcoord.BD09, // 当前坐标系
        //       gcoord.GCJ02, // 目标坐标系
        //     );
        //     rstObj.data[p].longitude = resae[0]
        //     rstObj.data[p].latitude = resae[1]
        //   }

        //   _this.setData({
        //     prolist: rstObj.data.list,
        //     prosum: rstObj.data.total
        //   })
        // })
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});
